/*
 * @Author: jiutian
 * @Date: 2023-01-31 10:43:22
 * @LastEditTime: 2023-03-13 09:10:37
 * @LastEditors:  
 */
/*
 * @Author: jiutian
 * @Date: 2023-01-31 10:43:22
 * @LastEditTime: 2023-02-02 14:21:49
 * @LastEditors:
 */
import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("@/views/entry/login.vue"),
  },
  {
    path: "/forget",
    name: "forget",
    meta: {
      title: "忘记密码",
    },
    component: () => import("@/views/entry/forget.vue"),
  },
  {
    path: "/homePage",
    name: "homePage",
    component: () => import("@/views/homePage/index.vue"),
    redirect: "/homePage/home",
    children: [
      {
        path: "home",
        name: "home",
        meta: {
          title: "首页",
        },
        component: () => import("@/views/homePage/home/index.vue"),
      },
      {
        path: "publish",
        name: "publish",
        meta: {
          title: "发布",
        },
        component: () => import("@/views/homePage/publish/index.vue"),
      },
      {
        path: "blogosphere",
        name: "blogosphere",
        meta: {
          title: "博客圈",
        },
        component: () => import("@/views/homePage/blogosphere/index.vue"),
      },
      {
        path: "person/:id",
        name: "person",
        meta: {
          title: "个人中心",
        },
        component: () => import("@/views/homePage/person/index.vue"),
      },
      {
        path: "blogDetail/:id",
        name: "blogDetail",
        meta: {
          title: "博客详情",
        },
        component: () => import("@/views/homePage/blogDetail/index.vue"),
      },
      {
        path: "search",
        name: "search",
        meta: {
          title: "博客圈",
        },
        component: () => import("@/views/homePage/search/index.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  let whiltList = ["login", "forget"];
  let findResult = whiltList.find((v) => v == to.name);
  let token = localStorage.getItem("token");
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (!findResult && !token) {
    next({ name: "login" });
    return NProgress.done();
  }
  next();
});

router.afterEach((to, from) => {
  NProgress.done();
});

export default router;
